package com.global.college.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.base.BaseActivity;
import com.global.college.ui.interfaces.IModifyPswView;
import com.global.college.ui.presenter.ModifyPswPresenter;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.RxTimer;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 */
public class ForgetPswActivity extends BaseActivity implements IModifyPswView {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.iv_cipherText)
    ImageView ivCipherText;
    @BindView(R.id.ll_psw_show_hint)
    LinearLayout llPswShowHint;
    @BindView(R.id.tv_modify_psw)
    TextView tvModifyPsw;
    private ModifyPswPresenter modifyPswPresenter;
    private RxTimer rxTimer;
    private boolean isVisible = true;
    private int i = 60;
    private boolean isSend = false;
    private String strPhone, strPassword, strCode;
    @Override
    public int getContentViewId() {
        return R.layout.activity_forget_psw;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(false);
        ImmersionBar.with(this)
                .statusBarColor(R.color.login_title)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void getInitData() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        modifyPswPresenter = new ModifyPswPresenter(this);
        modifyPswPresenter.setModifyPswView(this);
        initListener();
        tvModifyPsw.setEnabled(false);
    }
    private void initListener() {
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strPhone = charSequence.toString();
                strCode = etSmsCode.getText().toString();
                strPassword = etPsw.getText().toString();
                if (StringUtils.isNotEmpty(strPhone, true) && StringUtils.isNotEmpty(strCode, true) && StringUtils.isNotEmpty(strPassword, true)) {
                    tvModifyPsw.setEnabled(true);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_blue);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvModifyPsw.setEnabled(false);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_grey);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etSmsCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                strPhone = etPhone.getText().toString();
                strCode = charSequence.toString();
                strPassword = etPsw.getText().toString();
                if (StringUtils.isNotEmpty(strPhone, true) && StringUtils.isNotEmpty(strCode, true) && StringUtils.isNotEmpty(strPassword, true)) {
                    tvModifyPsw.setEnabled(true);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_red);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvModifyPsw.setEnabled(false);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_grey);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        etPsw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                strPhone = etPhone.getText().toString();
                strCode = etSmsCode.getText().toString();
                strPassword = charSequence.toString();
                if (StringUtils.isNotEmpty(strPhone, true) && StringUtils.isNotEmpty(strCode, true) && StringUtils.isNotEmpty(strPassword, true)) {
                    tvModifyPsw.setEnabled(true);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_red);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                } else {
                    tvModifyPsw.setEnabled(false);
                    tvModifyPsw.setBackgroundResource(R.drawable.selector_button_grey);
                    tvModifyPsw.setTextColor(getResources().getColor(R.color.white));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }
    @Override
    public void onFindPswSuccess(Object resetPswModel) {
        ToastUtils.showCenterToast("操作成功！");
        finish();
    }

    @Override
    public void onFindPswFail(String msg) {

    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {

    }

    @Override
    public void onGetSmsCodeFail(String msg) {

    }

    @Override
    public void editPwdSuccess(Object smsModel) {
        ToastUtils.showCenterToast("操作成功！");
        finish();

    }

    @Override
    public void editPwdFail(Object smsModel) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_register,R.id.tv_send_code, R.id.ll_psw_show_hint, R.id.tv_modify_psw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                openActivity(RegistActivity.class);
                break;
            case R.id.tv_send_code:
                getCode();
                break;
            case R.id.ll_psw_show_hint:
                if (isVisible) {
                    //密码输入框的id login_pwd
                    etPsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    isVisible = false;
                    ivCipherText.setBackgroundResource(R.mipmap.icon_show_psw);
                    etPsw.setSelection(etPsw.length());
                } else {
                    etPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    isVisible = true;
                    ivCipherText.setBackgroundResource(R.mipmap.icon_hint_psw);
                    etPsw.setSelection(etPsw.length());
                }
                break;
            case R.id.tv_modify_psw:
                modifyPsw();
                break;
        }
    }
    /**
     * 获取验证码
     */
    private void getCode() {
        strPhone = etPhone.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils.isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (!StringUtils.isPhone(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (isSend){
                ToastUtils.showToastShort(R.string.please_wait_moment);
                return;
            }
            //发送
            modifyPswPresenter.getSmsCode(strPhone);
            startTimer();
        }
    }

    /**
     * 忘记密码
     */
    private void modifyPsw() {
        strPhone = etPhone.getText().toString();
        strPassword = etPsw.getText().toString();
        strCode = etSmsCode.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils
                    .isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            } else if (6 > StringUtils.getLength(strPassword, true)
                    || 16 < StringUtils.getLength(strPassword, true)) {
                ToastUtils.showToastShort(
                        R.string.please_input_from_six_to_sixteen_psw);
                return;
            } else if (StringUtils.isEmpty(strCode)) {
                ToastUtils.showToastShort(
                        R.string.sms_code_not_empty);
                return;
            } else if (!StringUtils.isNumber(strCode)) {
                ToastUtils.showToastShort(R.string.sms_code_format_error);
                return;
            } else {
                //忘记密码
                modifyPswPresenter.findPsw(strPhone, strCode,strPassword);
            }
        }
    }
    /**
     * 开始计时任务
     */
    private void startTimer() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number) {
                Log.e("打印时间：","----"+number);
                if (number == i) {
                    tvSendCode.setText(R.string.resend);
                    isSend = false;
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_fff73043));
                    rxTimer.cancel();

                } else {
                    tvSendCode.setText("(" + (i-number) + ")"+getString(R.string.sended));
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_999999));
                    isSend = true;
                }
            }
        });
    }
}